import axios from 'axios'
import qs from 'qs'

axios.defaults.timeout = 5000;                        //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';        //配置请求头
axios.defaults.baseURL = 'http://172.18.248.68:9001/jieao';//   //配置接口地址http://10.0.0.250:9001/jieao_manage   http://localhost:8080/jieao

//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
	//在发送请求之前做某件事
	if(config.method  === 'post'){
		// config.data = qs.stringify(config.data);
		if(config.headers['Content-Type'] !== 'application/x-www-form-urlencoded'){
			config.data = qs.stringify(config.data);
		}else{
			config.data = config.data;
		}
	}
	return config;
},(error) =>{
	return Promise.reject(error);
});

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) =>{
	//对响应数据做些事
	if(!res.data.success){
		return Promise.resolve(res);
	}
	return res;
}, (error) => {
	return Promise.reject(error);
});

//返回一个Promise(发送post请求)
export function fetchPost(url, params) {
	return new Promise((resolve, reject) => {
		axios.post(url, params)
			.then(response => {
				resolve(response);
			}, err => {
				reject(err);
			})
			.catch((error) => {
				reject(error)
			})
	})
}
////返回一个Promise(发送get请求)
export function fetchGet(url, param) {
	return new Promise((resolve, reject) => {
		axios.get(url, {params: param})
			.then(response => {
				resolve(response)
			}, err => {
				reject(err)
			})
			.catch((error) => {
				reject(error)
			})
	})
}
//发送文件
export function postFile(url,files) {
	let instance = axios.create();
	instance.defaults.timeout = 30000;
	let forms = new FormData()
	for (let i = 0; i < files.length; i++) {
		forms.append('file',files[i].file)
	}
	instance.defaults.headers.post['Content-Type'] = 'multipart/form-data';
	return new Promise((resolve, reject) => {
		instance.post(url, forms)
		.then(response => {
			resolve(response);
		}, err => {
			reject(err);
		})
		.catch((error) => {
			reject(error)
		})
	})
}
export default {
	fetchPost,
	fetchGet,
	postFile
}
